package com.woniu.supermarket.web;


import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Promotion;
import com.woniu.supermarket.entity.PromotionCheck;
import com.woniu.supermarket.entity.PromotionDetail;
import com.woniu.supermarket.entity.Type;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IPromotionCheck;
import com.woniu.supermarket.service.IPromotionDetailService;
import com.woniu.supermarket.service.IPromotionService;
import com.woniu.supermarket.service.ITypeService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.PromotionDetailVo;
import com.woniu.supermarket.vo.PromotionVo;
import com.woniu.supermarket.vo.PromotionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("promotion")
@Api(value = "促销的操作")
public class PromotionController {
    
    @Autowired
    IPromotionService iPromotionService;

    @Autowired
    IPromotionDetailService iPromotionDetailService;

    @Autowired
    IPromotionCheck iPromotionCheck;

    @Autowired
    ITypeService iTypeService;
    
    //分页查询
    @GetMapping("/plist")
    @ApiOperation(value = "分页查询促销")
    public ResultResponse<PageInfo<PromotionVo>> clist(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size,
            @RequestParam(value = "code",defaultValue = "")String code,
            @RequestParam(value = "status",defaultValue = "-1")int status){


        ResultResponse<PageInfo<PromotionVo>> result = null;
        try {
            PageInfo<PromotionVo> pageInfo = iPromotionService.queryByPage(now, size, code,status);
            result = new ResultResponse<>(200,"查询促销成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询促销失败!");
        }
        return result ;
    }

/*    //分页查询
    @GetMapping("/promotionlist")
    @ApiOperation(value = "分页查询促销")
    public ResultResponse<PageInfo<Promotion>> promotionlist(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size,
            @RequestParam(value = "code",defaultValue = "")String code,
            @RequestParam(value = "status",defaultValue = "-1")int status){


        ResultResponse<PageInfo<Promotion>> result = null;
        try {
            PageInfo<Promotion> pageInfo = iPromotionService.queryByPage(now, size, code,status);
            result = new ResultResponse<>(200,"查询促销成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询促销失败!");
        }
        return result ;
    }*/

    @PostMapping("/add")
    @ApiOperation(value = "添加促销")
    public ResultResponse<Void> add(@RequestBody PromotionVo promotionVo){
        ResultResponse<Void> result = null;

        try {
            Promotion promotion= new Promotion();
            promotion.setPromotionNumber(promotionVo.getPromotionNumber());
            Type promotionType = promotionVo.getPromotionType();
            iTypeService.add(promotionType);
            promotion.setPromotionType(promotionType.getId());
            promotion.setPromotionShop(promotionVo.getPromotionShop().getId());
            promotion.setPromotionStartTime(promotionVo.getPromotionStartTime());
            promotion.setPromotionEndTime(promotionVo.getPromotionEndTime());
            promotion.setPromotionStaff(promotionVo.getPromotionStaff());
            promotion.setStatus(0);
            promotion.setPromotionStatus(2);
            iPromotionService.add(promotion);
            List<PromotionDetailVo> list = promotionVo.getPromotionDetailVoList();
            PromotionDetail pd = new PromotionDetail();
            pd.setPromotionId(promotion.getId());
            for(PromotionDetailVo promotionDetail:list){
                pd.setPromotionDetailNumber(CodeCreateUtil.createPromotionDetailCode());
                pd.setGoodsId(promotionDetail.getId());
                iPromotionDetailService.add(pd);
            }
            result = new ResultResponse<>(200,"添加促销成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加促销失败!");
        }
        return result ;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据id删除促销")
    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {

            iPromotionService.remove(id);
            //同时删除详情表中相关数据
            iPromotionDetailService.removeByPromotionId(id);
            result = new ResultResponse<>(200,"删除促销成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除促销失败!");
        }
        return result ;
    }


    @PutMapping("/modify")
    @ApiOperation(value = "修改促销")
    public ResultResponse<Void> modify(@RequestBody PromotionVo promotionVo){

        ResultResponse<Void> result = null;

        try {
            Promotion promotion= new Promotion();
            promotion.setId(promotionVo.getId());
            promotion.setPromotionNumber(promotionVo.getPromotionNumber());
            Type promotionType = promotionVo.getPromotionType();
            iTypeService.modify(promotionType);
            promotion.setPromotionType(promotionType.getId());
            promotion.setPromotionShop(promotionVo.getPromotionShop().getId());
            promotion.setPromotionStartTime(promotionVo.getPromotionStartTime());
            promotion.setPromotionEndTime(promotionVo.getPromotionEndTime());
            promotion.setPromotionStaff(promotionVo.getPromotionStaff());
            promotion.setStatus(0);
            promotion.setPromotionStatus(2);
            iPromotionService.modify(promotion);
            Integer id = promotion.getId();
            //删除有关的明细，在重新添加
            iPromotionDetailService.deleteByPromotionId(id);
            List<PromotionDetailVo> list = promotionVo.getPromotionDetailVoList();
            PromotionDetail pd = new PromotionDetail();
            pd.setPromotionId(id);
            for(PromotionDetailVo promotionDetail:list){
                pd.setPromotionDetailNumber(CodeCreateUtil.createPromotionDetailCode());
                pd.setGoodsId(promotionDetail.getGoodsVo().getId());
                iPromotionDetailService.add(pd);
            }

            result = new ResultResponse<>(200,"修改促销成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改促销失败!");
        }
        return result ;
    }

    //查询审核详情
    @GetMapping("/showPromotionCheck")
    @ApiOperation(value = "查询审核详情")
    public ResultResponse<List<PromotionCheck>> checkList(@RequestParam(value = "id") Integer id){


        ResultResponse<List<PromotionCheck>> result = null;
        System.out.println("查询审核详情"+id);
        try {
            List<PromotionCheck> list = iPromotionCheck.queryByPromotionId(id);
            result = new ResultResponse<>(200,"查询成功!",list);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }


    @PostMapping("/addCheck")
    @ApiOperation(value = "添加促销审核记录")
    public ResultResponse<Void> addCheck(@RequestBody PromotionCheck promotionCheck){
        ResultResponse<Void> result = null;

        try {
            promotionCheck.setCheckTime(new Date());
            iPromotionCheck.add(promotionCheck);
            if(promotionCheck.getCheckResult()==1){
                Promotion p =iPromotionService.queryById(promotionCheck.getPromotionId());
                p.setStatus(1);
                iPromotionService.modify(p);
            }
            result = new ResultResponse<>(200,"添加促销审核记录成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加促销审核记录失败!");
        }
        return result ;
    }


}
